// 加密key，与后台保持一致
const CRYPTOJSKEY = "ih9/kVkk9wO1YvUXB/LNrQ=="

import CryptoJS from 'crypto-js'

export function AESEncrypt(text) {
	const key = CryptoJS.enc.Utf8.parse(CRYPTOJSKEY)

	const encryptedData = CryptoJS.AES.encrypt(text, key, {
		mode: CryptoJS.mode.ECB,
		padding: CryptoJS.pad.Pkcs7
	})

	let encryptedBase64Str = encryptedData.ciphertext.toString(CryptoJS.enc.Base64)
	
	encryptedBase64Str = encryptedBase64Str.replace(/\r/g, "").replace(/\n/g, "")

	return encryptedBase64Str
}

export function AESDecrypt(text) {
	text = decodeURIComponent(text)	
	const ciphertext = text.replace(/\r/g, "").replace(/\n/g, "")

	const key = CryptoJS.enc.Utf8.parse(CRYPTOJSKEY)

	const encrypted = CryptoJS.AES.decrypt({
		ciphertext: CryptoJS.enc.Base64.parse(ciphertext)
	}, key, {
		mode: CryptoJS.mode.ECB,
		padding: CryptoJS.pad.Pkcs7
	})

	return encrypted.toString(CryptoJS.enc.Utf8)
}